﻿using System;

namespace SignalRDemo.Common.Extensions
{
    public static class StringExtensions
    {
        /// </summary>
        /// <param name="dt">时间</param>
        /// <returns>格式化后的日期字符串</returns>
        public static string ToCNTimeString(this DateTime dt)
        {
            var str = dt.ToString("yyyy年MM月dd日");
            return str;
        }

        /// <summary>
        /// 格式化时间，格式化如下(2009-6-7 1:43:04)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ToXmlTimeString(this DateTime dt)
        {
            var str = dt.ToString("yyyy-MM-dd HH:mm:ss");
            return str;
        }


        public static float ToXmlFloat(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return default(float);
            }
            if (float.TryParse(value, out float f))
            {
                return f;
            }
            else
            {
                return default(float);
            }
        }

        public static int ToXmlInt(this string value)
        {
            if (value.IsNullOrEmpty())
            {
                return default(int);
            }
            if (int.TryParse(value, out int f))
            {
                return f;
            }
            else
            {
                return default(int);
            }

        }
    }
}
